<template>
  <div class="wrapper">
    <div
        style="margin: 170px auto; background-color: #fff; width: 350px; height: 400px; padding: 20px; border-radius: 10px">
      <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>登 录</b></div>
      <el-form :rules="rules" :model="user" ref="userDtoForm">
        <el-form-item prop="username">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-user" v-model="user.username" placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input size="medium" style="margin: 10px 0" prefix-icon="el-icon-lock" show-password placeholder="请输入密码"
                    v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item>
         <div style="display: flex">
           <el-input size="medium" style="margin: 10px 0;width: 200px" prefix-icon="el-icon-key" placeholder="请输入验证码"
                     v-model="user.validCode"></el-input>
           <ValidCode @input="createValidCode" style="margin-top: 8px"/>
         </div>
        </el-form-item>
        <el-form-item style="margin: 10px 0; text-align: center">
          <el-button type="primary" size="small" autocomplete="off" @click="login" style="width: 100%;">登录</el-button>
          <el-button type="text" size="small" autocomplete="off" @click="$router.push('/register')" style="margin-right: 15px;">没有账号？点击注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

import ValidCode from "@/components/ValidCode";
export default {
  name: "Login",
  components: {ValidCode},
  data() {
    return {
      validCode:'',
      user: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur'}
        ],
      }
    }
  },
  created() {
    //如果是注册完后跳转就自动填写用户名和密码
    if (localStorage.getItem("registerUser")) {
      let registerUser=JSON.parse(localStorage.getItem("registerUser"));
      this.user.username=registerUser.username;
      this.user.password=registerUser.password;
    }
  },
  methods: {
    //登录
    login() {
      //格式验证
      this.$refs["userDtoForm"].validate((valid) => {
        //验证码
        if (!this.user.validCode) {
          this.$message.warning("请填写验证码")
          return
        }
        if (this.user.validCode.toLowerCase()!==this.validCode.toLowerCase()) {
          this.$message.error("验证码错误")
          return
        }
        if (valid) {
          this.request.post("/user/login",this.user).then(res=>{
            if(res.code!='200') {
              this.$message.error(res.msg)
            }
            else {
              localStorage.setItem("user",JSON.stringify(res.data))
              this.$message.success("登陆成功")
              this.$router.push("/")
              if (localStorage.getItem("registerUser"))
                localStorage.removeItem("registerUser")
              //和服务器端建立会话连接
              sessionStorage.setItem("user", JSON.stringify(res.data))
            }
          })
        }
      });
    },
    // 设置验证码为validCode.vue返回的数据
    createValidCode(data) {
      this.validCode=data
    }
  }
}
</script>

<style>
.wrapper {
  height: 100vh;
  background-image: linear-gradient(to bottom right, #FC466B, #3F5EFB);
  overflow: hidden;
}
</style>
